<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>智慧芜湖港</title>
    <link rel="stylesheet" href="/static/css/aui.css">
    <link rel="stylesheet" href="/static/css/use_1.css">
    <link rel="shortcut icon" href="/static/img/aaa.png" />
    <script src="/static/js/vue.min.js"></script>
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script src="/static/js/axios.min.js"></script>
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="/static/js/index.js"></script>
    <style>
        .tip_zero_time {
            width: 100%;
            height: 40vw;
            /* background-color: red; */
            z-index: 2;
        }
        
        * {
            margin: 0, 0;
            padding: 0, 0;
        }
    </style>
</head>

<body>
    <div id="top">
        <div id="left">
            <div id="left_top">
                <div id="left_top_img">
                    <img src="">
                </div>
                <div id="left_top_name">
                    <%=data.DriverName%>
                </div>
            </div>
            <div id="left_bottom">
                <%=data.PhoneNum%>
            </div>
        </div>
        <div id="right">
            <div id="app">
                <button type="button" onclick="EXIT()">退出</button>
            </div>
            <div id="right_bottom">
                <%=data.FleetCName%>
            </div>
        </div>
    </div>
    <div id="middle">
        大 桥<text style="color: red;"> 优 惠 </text>证
    </div>
    <div id="bottom">
        <div id="time">
            <!-- 有效时间：<text style="color: red;">7/28 14:00前</text> -->
        </div>
        <div id="card">
            <%=data.TruckLic%>
        </div>
        <div id="youhui">
            <text id="youhui_1" style="color: red; font-size: 40px;"></text><text id="youhui_2"></text>
        </div>
        <div id="content">
            <div id="use" onclick="use()">
                立即<br>使用
            </div>
        </div>
        <div id="text">
            点击使用后请在10分钟后通行!
        </div>
    </div>
    <div id="u">

    </div>
</body>
<script src="/static/js/aui-toast.js"></script>
<script>
    var toast = new auiToast();
    let jwtToken = document.cookie.split('=')[1]
    var DriverName;
    var FleetCName;
    var PhoneNum;
    var TruckLic;
    // window.onload = function() {
    DriverName = document.getElementById("left_top_name").innerText;
    FleetCName = document.getElementById("right_bottom").innerText;
    PhoneNum = document.getElementById("left_bottom").innerText;
    TruckLic = document.getElementById("card").innerText;
    // }
    Date.prototype.Format = function(fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份 
                "d+": this.getDate(), //日 
                "H+": this.getHours(), //小时 
                "m+": this.getMinutes(), //分 
                "s+": this.getSeconds(), //秒 
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
                "S": this.getMilliseconds() //毫秒 
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
        //结束时间计算
    function newtime(startDate) {
        if (startDate.length > 0) {
            var startDateTemp = startDate.split(" ");
            var arrStartDate = startDateTemp[0].split("-");
            var arrStartTime = startDateTemp[1].split(":");
            var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);
            var count = (allStartDate.getTime() + 1000 * 24 * 60 * 60);
            return count;
        } else {
            alert("时间不能为空");
        }
    }
    var start = new Date().Format("yyyy-MM-dd HH:mm:ss");
    var oldTime = (new Date(start)).getTime() + 24 * 60 * 60 * 1000; //得到毫秒数  
    var end = new Date(oldTime).Format("yyyy-MM-dd HH:mm:ss"); //就得到普通的时间了 
    var CouponID_Use;
    var json;
    // const HTTP = new XMLHttpRequest();
    var data = {
        "TruckLic": '<%=data.TruckLic%>',
        "Direction": '<%=Direction%>',
        "StartTime": start,
        "EndTime": end,
    };
    /**
     * 请求链接需要修改
     * 
     * 
     */
    axios({
            headers: {
                authorization: jwtToken
            },
            method: 'post',
            url: '/api/getCouponList',
            data: data
        })
        .then(response => {
            json = response.data
            var timenow = new Date().Format("yyyy-MM-dd HH:mm:ss");
            if (json.code == 1) {
                // CouponID_Use = getCouponID(json.data, timenow); 
                //最先使用的优惠券ID
                var len = json.data.length;
                var serialNum = len - 1;
                CouponID_Use = json.data[serialNum].CouponID;
                StatusNow_Use = json.data[serialNum].StatusNow;
                ReceiptId_Use = json.data[serialNum].ReceiptID;

                // //是否有正在使用的优惠券
                var isu = isusing(json.data, timenow);
                if (isu[0]) {
                    window.location.replace('/static/use.html?time=' + isu[1] + '&LastRedeemTime=' + (600 - gettimec(timenow, isu[1])[1]) + '&DriverName=' + encodeURI(encodeURI(DriverName)) + '&FleetCName=' + encodeURI(encodeURI(FleetCName)) + '&PhoneNum=' + encodeURI(encodeURI(PhoneNum)) + '&TruckLic=' + encodeURI(encodeURI(TruckLic)));
                } else {
                    //limit_flag 是否达到上线标志
                    var limit_flag_in = '<%=limit_flag_in%>' == 'true' ? true : false;
                    var limit_flag_out = '<%=limit_flag_out%>' == 'true' ? true : false;
                    if (limit_flag_in && '<%=Direction%>' == '0' || limit_flag_out && '<%=Direction%>' == '1') {
                        document.getElementById('youhui').style.display = 'none';
                        document.getElementById('content').style.display = 'none';
                        document.getElementById('text').style.display = 'none';
                        let tip_zero_time = document.createElement('div');
                        let fatherElement = document.getElementById('bottom');
                        fatherElement.appendChild(tip_zero_time);
                        tip_zero_time.setAttribute('class', 'tip_zero_time');
                        tip_zero_time.innerHTML = '<p style="margin: 0 auto;font-size: 25px;text-align: center;">您来晚了,</p><p style = "margin: 0 auto;font-size: 25px;text-align: center;">当日优惠次数<span style = "color: red;">已用完~</span></p>'
                    } else {
                        var times = json.data.length;
                        var div = document.getElementById('youhui_1');
                        var div2 = document.getElementById('youhui_2');
                        if ('<%=Direction%>' == '0') {
                            div.innerHTML = "进";
                        } else if ('<%=Direction%>' == '1') {
                            div.innerHTML = "出";
                        }
                        div2.innerHTML = times + " 次优惠"
                    }

                }
            } else {
                var div = document.getElementById('youhui_1');
                var div2 = document.getElementById('youhui_2');
                if ('<%=Direction%>' == '0') {
                    div.innerHTML = "进";
                } else if ('<%=Direction%>' == '1') {
                    div.innerHTML = "出";
                }
                // div2.innerHTML = 0 + " 次优惠"
                document.getElementById('youhui').style.display = 'none';
                document.getElementById('content').style.display = 'none';
                document.getElementById('text').style.display = 'none';
                let tip_zero_time = document.createElement('div');
                let fatherElement = document.getElementById('bottom');
                fatherElement.appendChild(tip_zero_time);
                tip_zero_time.setAttribute('class', 'tip_zero_time');
                tip_zero_time.innerHTML = '<p style="margin: 0 auto;font-size: 25px;text-align: center;">很遗憾,</p><p style = "margin: 0 auto;font-size: 25px;text-align: center;"><span style = "color: red;"> 没有 </span>可以使用的优惠~</p>'

            }
        })
        .catch(function(error) {
            if (error.response) {
                console.log(error.response.data);
                toast.fail({
                    title: error.response.data.msg,
                    duration: 2000
                })
            }
        });
    //判断是否有正在使用的优惠券
    function isusing(data, timenow) {
        var isusingres = [];
        isusingres[0] = false;
        isusingres[1] = '';
        data.forEach(function(item) {
            if (item.LastRedeemTime != '' && gettimec(timenow, item.LastRedeemTime)[0]) {
                isusingres[0] = true;
                isusingres[1] = item.LastRedeemTime;
                return isusingres;
            }
        })
        return isusingres;
    }

    function EXIT() {
        var told = confirm("确定退出嘛？");
        if (told) {
            WeixinJSBridge.call('closeWindow');
        }
    }
    //使用优惠券
    function use() {
        //是否有正在使用的优惠券 
        var timenow = new Date().Format("yyyy-MM-dd HH:mm:ss");
        var isu = isusing(json.data, timenow);
        if (isu[0]) {
            window.location.replace('/static/use.html?time=' + isu[1] + '&LastRedeemTime=' + (600 - gettimec(timenow, isu[1])[1]) + '&DriverName=' + encodeURI(encodeURI(DriverName)) + '&FleetCName=' + encodeURI(encodeURI(FleetCName)) + '&PhoneNum=' + encodeURI(encodeURI(PhoneNum)) + '&TruckLic=' + encodeURI(encodeURI(TruckLic)));
        } else {
            var timeuse = new Date().Format("yyyy-MM-dd HH:mm:ss");
            const HTTP = new XMLHttpRequest();
            HTTP.withCredentials = true;
            var data = {
                "StatusNow": StatusNow_Use,
                "ReceiptID": ReceiptId_Use,
                "CouponID": CouponID_Use,
                "Direction": '<%=Direction%>',
                "RedeemTime": timeuse,
                "PhoneNum": PhoneNum,
                "Channel": '<%=Channel%>',
                TruckLic
            };
            axios({
                    headers: {
                        authorization: jwtToken
                    },
                    method: 'put',
                    url: '/api/useCoupon',
                    data: data
                })
                .then(response => {
                    let json = response.data
                    if (json.code == 1) {
                        window.location.replace('/static/use.html?time=' + timenow + '&LastRedeemTime=' + 600 + '&DriverName=' + encodeURI(encodeURI(DriverName)) + '&FleetCName=' + encodeURI(encodeURI(FleetCName)) + '&PhoneNum=' + encodeURI(encodeURI(PhoneNum)) + '&TruckLic=' + encodeURI(encodeURI(TruckLic)));
                    } else {
                        document.getElementById('youhui').style.display = 'none';
                        document.getElementById('content').style.display = 'none';
                        document.getElementById('text').style.display = 'none';
                        let tip_zero_time = document.createElement('div');
                        let fatherElement = document.getElementById('bottom');
                        fatherElement.appendChild(tip_zero_time);
                        tip_zero_time.setAttribute('class', 'tip_zero_time');
                        tip_zero_time.innerHTML = '<p style="margin: 0 auto;font-size: 25px;text-align: center;">您来晚了,</p><p style = "margin: 0 auto;font-size: 25px;text-align: center;">当日优惠次数<span style = "color: red;">已用完~</span></p>'
                    }
                })
                .catch(function(error) {
                    if (error.response) {
                        console.log(error.response.data);
                        toast.fail({
                            title: error.response.data.msg,
                            duration: 2000
                        })
                    }
                });


        }


    }
    //获取最早的优惠券
    function getCouponID(json, nowtime) {
        var time = nowtime;
        var Couponid = json[0].CouponID;
        json.forEach(function(item) {
            if (compareTime(item.ActiveTime, time)) {
                time = item.ActiveTime;
                Couponid = item.CouponID;
            }
        });
        return Couponid;
    }

    //比较时间大小
    function compareTime(startDate, endDate) {
        if (startDate.length > 0 && endDate.length > 0) {
            var startDateTemp = startDate.split(" ");
            var endDateTemp = endDate.split(" ");

            var arrStartDate = startDateTemp[0].split("-");
            var arrEndDate = endDateTemp[0].split("-");

            var arrStartTime = startDateTemp[1].split(":");
            var arrEndTime = endDateTemp[1].split(":");

            var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);
            var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);

            if (allStartDate.getTime() >= allEndDate.getTime()) {
                return false;
            } else {
                return true;
            }
        } else {
            alert("时间不能为空");
        }
    }

    //判断是否在十分钟有效期内
    function gettimec(startDate, endDate) {
        var getc = [];
        if (startDate.length > 0 && endDate.length > 0) {
            var startDateTemp = startDate.split(" ");
            var endDateTemp = endDate.split(" ");

            var arrStartDate = startDateTemp[0].split("-");
            var arrEndDate = endDateTemp[0].split("-");

            var arrStartTime = startDateTemp[1].split(":");
            var arrEndTime = endDateTemp[1].split(":");

            var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);
            var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);
            var count = (allStartDate.getTime() - allEndDate.getTime()) / 1000;
            // alert(count)
            if (count <= 10 * 60) {
                // return true;
                getc[0] = true;
                getc[1] = count;
            } else {
                // return false;
                getc[0] = false;
                getc[1] = count;
            }
            return getc;
        } else {
            alert("时间不能为空");
        }
    }
</script>

</html>